/*
 * Log.cpp
 *
 *  Created on: Jul 21, 2013
 *      Author: Dan
 */

#include "Log.h"

std::string Log::theLogFile = "";
bool Log::isDebugOn = false;

void Log::setFile(std::string aFileName) {
	std::ofstream test;
	test.open(aFileName.c_str(), std::ofstream::out | std::ofstream::app);
	if (test.good()) {
		Log::theLogFile = aFileName;
	} else {
		std::cout << "FAILED TO OPEN LOG FILE: " << aFileName << std::endl;
	}
	test.close();
}

void Log::setDebugMode(bool isOn) {
	Log::isDebugOn = isOn;
}

void Log::event(std::string anEvent) {
	output(anEvent);
}

void Log::output(std::string aString) {
	if (theLogFile != "") {
		std::ofstream file;
		file.open(theLogFile.c_str(), std::ofstream::out | std::ofstream::app);
		if (file.good()) {
			file << aString << std::endl;
		} else {
			std::cout << aString << std::endl;
		}
		file.close();
	} else {
		std::cout << aString << std::endl;
	}
}

void Log::error(std::string anError) {
	output("ERROR: " + anError);
}

void Log::debug(std::string anEvent) {
	if (Log::isDebugOn) {
		output(anEvent);
	}
}
